﻿Public Partial Class TruckTypeMaster
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            ViewState("editID") = ""
            BindData()
        End If
    End Sub
    Protected Sub BindData(Optional ByVal searchText As String = "")
        Dim db As New DB_Class()
        db.queryString = "select *,m.MATERIAL+'-'+MATL_DESC as mat" & _
        " from Truck_Type_Master ttm left join Meterial m on ttm.MATERIAL=m.MATERIAL where isDel <> 'YES'"
        If (searchText <> "") Then
            db.queryString &= " and TruckType like '%" & searchText & "%'"
        End If
        Dim dt As DataTable = db.getTable("TruckType")
        ViewState("TruckType") = dt
        truckTypeGrid.DataSource = dt
        truckTypeGrid.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = truckTypeGrid.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                truckTypeGrid.PageIndex = 0
            Case "prev"
                If (truckTypeGrid.PageIndex <> 0) Then
                    truckTypeGrid.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (truckTypeGrid.PageIndex <> truckTypeGrid.PageCount) Then
                    truckTypeGrid.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                truckTypeGrid.PageIndex = truckTypeGrid.PageCount
        End Select
        truckTypeGrid.DataSource = ViewState("TruckType")
        truckTypeGrid.DataBind()
    End Sub
    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = truckTypeGrid.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        truckTypeGrid.PageIndex = ddlPage.SelectedIndex
        truckTypeGrid.DataSource = ViewState("TruckType")
        truckTypeGrid.DataBind()
    End Sub

    Private Sub btnAddTruckType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddTruckType.Click
        tableDataPanel.Visible = False
        formPanel.Visible = True
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtTruckType.Text <> "" And ddlMeterial.SelectedValue <> "") Then
            Dim editId As String = ViewState("editID").ToString()
            Dim db As New DB_Class()
            Dim qStr As String = Request.Url.Query
            If (editId = "") Then
                Dim truckTypeId As String = db.GeneratID("Truck_Type_Master", "TruckTypeID")
                db.queryString = String.Format("insert into Truck_Type_Master (TruckTypeID,TruckType,MATERIAL) values('{0}','{1}','{2}')", truckTypeId, txtTruckType.Text.Trim(), "00000000" & ddlMeterial.SelectedValue)
            Else
                db.queryString = String.Format("update Truck_Type_Master set TruckType='{0}',MATERIAL='{1}' where TruckTypeID='" & editId & "'", txtTruckType.Text.Trim(), "00000000" & ddlMeterial.SelectedValue)
            End If

            If (db.insertUpdate() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลสำเร็จ'); window.location='TruckTypeMaster.aspx" & qStr & "'", True)

                'Response.Redirect("~/Master_Form/TruckTypeMaster.aspx" + qStr)
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่สามารถบันทึกข้อมูลได้'); window.location='TruckTypeMaster.aspx" & qStr & "'", True)
                'Dim qStr As String = Request.Url.Query
                'Response.Redirect("~/Master_Form/TruckTypeMaster.aspx" + qStr)
            End If

        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาระบุข้อมูลให้ครบ !!');", True)
            Exit Sub
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim qStr As String = Request.Url.Query
        Response.Redirect("~/Master_Form/TruckTypeMaster.aspx" + qStr)
    End Sub

    Private Sub truckTypeGrid_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles truckTypeGrid.RowDeleting
        Dim delId As String = truckTypeGrid.DataKeys(e.RowIndex)("TruckTypeID").ToString()
        Dim db As New DB_Class()
        db.queryString = "update Truck_Type_Master set isDel='YES' where TruckTypeID='" & delId & "'"
        Dim qStr As String = Request.Url.Query
        If (db.insertUpdate() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลนี้แล้ว'); window.location='TruckTypeMaster.aspx" & qStr & "'", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่สามารถลบข้อมูลนี้ได้'); window.location='TruckTypeMaster.aspx" & qStr & "'", True)
        End If



    End Sub

    Private Sub truckTypeGrid_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles truckTypeGrid.RowEditing
        Dim editIndex As Integer = e.NewEditIndex
        ViewState("editID") = truckTypeGrid.DataKeys(editIndex)("TruckTypeID").ToString()
        Dim strMaterial As String = truckTypeGrid.DataKeys(editIndex)("MATERIAL").ToString()
        If (strMaterial <> "") Then
            strMaterial = strMaterial.Substring(8)
        End If
        txtTruckType.Text = truckTypeGrid.DataKeys(editIndex)("TruckType").ToString()
        ddlMeterial.ClearSelection()
        ddlMeterial.Items.FindByValue(strMaterial).Selected = True

        tableDataPanel.Visible = False
        formPanel.Visible = True
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        BindData(txtTypeSearch.Text)
    End Sub
End Class